package edu.guet.server.service;

import edu.guet.server.mapper.ResourceMapper;
import edu.guet.server.mapper.ShowResourceMapper;
import edu.guet.server.model.Resource;
import edu.guet.server.model.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ResourceService {
    @Autowired
    ResourceMapper resourceMapper;
    @Autowired
    ShowResourceMapper resourceMapper_xytx;
    /**
     * 添加学期课程资源
     * @param resource
     * @return
     */
    public Integer addResource(Resource resource)
    {
        resourceMapper.addResource(resource);
        return resource.getResource_id();
    }


    public void updateResource(Resource resource) {
        resourceMapper.updateCourseResource(resource);
//        return resource.getResource_id();
    }
    /**
     * 根据id获取resource
     * @param id
     * @return
     */
    public Resource getResourceById(Integer id) {
        return resourceMapper.findResourceById(id);
    }

    public Integer getTypeCountBySemesterId(String type, Integer semesterId){
        return resourceMapper.getTypeCountBySemesterId(type,semesterId);
    }

    /**
     * 添加单元视频
     *
     * @param name
     * @param url
     * @return resource_id
     */
    @Transactional
    public Integer addResourceUrl( String name,String url,String type) {
        Resource resource = new Resource();
        resource.setResource_url(url);
        resource.setResource_name(name);
        resource.setResource_type(type);
        resourceMapper.addResource(resource);
        return resource.getResource_id();
    }

    public void addTechRe(ResourceDTO resourceDTO) {
        resourceMapper.addTechRe(resourceDTO);
    }

    /**
     * 展示资源库视频列表
     *
     * @return
     */
    public List<ResourceDTO> showResources(Integer courseId, String type) {
        return resourceMapper.showResources(courseId,type);
    }

    public StuReDTO selectStuRe(StuReDTO stuRe) {
        return resourceMapper.selectStuRe(stuRe);
    }

    public void addStuResource(StuReDTO stuReDTO) {
        resourceMapper.addStuResource(stuReDTO);
    }

    public WatchUnitReDTO selectWatchResource(WatchUnitReDTO watchUnitReDTO) {
        return resourceMapper.selectWatchResource(watchUnitReDTO);
    }

    public void addWatchUniRe(WatchUnitReDTO watchUnitReDTO) {
        resourceMapper.addWatchUniRe(watchUnitReDTO);
    }

    public void updateWatchRe(WatchUnitReDTO watchUnitReDTO) {
        resourceMapper.updateWatchRe(watchUnitReDTO);
    }
    /**
     * 删除
     * @param
     * @return
     */
    public Integer deleteResource(Integer id){
        return resourceMapper.deleteResource(id);}

    /**
     * 修改名称
     * @param id
     * @param name
     */
    public void changeResourceName( Integer id, String name){
        resourceMapper.changeResourceName(id, name);}

    public List<CheckResource> getCheckResource(Integer unitId, Integer userId) {
        List<CheckResource> list=new ArrayList<>();
        //查询子模块id
        List<Integer> subUnitIds = resourceMapper_xytx.findSubUnitIdsByUnitId(unitId);
        //List<Integer> reIds=resourceMapper.getReIdsByUnitIds(unitId);
        List<Integer> reIds=resourceMapper_xytx.findResourceIdsBySubUnitIds(subUnitIds);
        for (Integer reId : reIds) {
            CheckResource checkResource = new CheckResource();
            Integer check=resourceMapper.getCountStuRe(unitId,userId,reId);
            checkResource.setCheck(check);
            checkResource.setResourceId(reId);
            list.add(checkResource);
        }
        return list;
    }

    public void deleteCourseResource(Integer resourceId) {
         resourceMapper.deleteCourseResource( resourceId);
    }

    public List<UnitReList> getUnitsRes(Integer semesterId) {
        List<UnitReList> list=resourceMapper.getUnitstitle(semesterId);
        log.info("list:"+list);
        for (UnitReList unitRe : list) {
            unitRe.setResourceDTOS(resourceMapper.findReByunitId(unitRe.getUnit_id()));
        }
        return list;
    }
}
